<template>
  <div class="home card">
    <div class="top">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="患者姓名">
          <el-input clearable v-model="formInline.patientName" placeholder="" style="width: 180px" />
        </el-form-item>
        <el-form-item label="随访方式">
          <el-select v-model="formInline.followUpMethod" placeholder="" style="width: 180px">
            <el-option v-for="item in ssfslist" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="随访目的 ">
          <el-select v-model="formInline.followUpPurpose" placeholder="" style="width: 180px">
            <el-option v-for="item in sfmdlist" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="随访状态">
          <el-select v-model="formInline.followUpStatus" placeholder="" style="width: 180px">
            <el-option v-for="item in sfstatus" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="随访日期">
          <el-date-picker v-model="formInline.followUpDate" value-format="YYYY-MM-DD" type="date" style="width: 240px" />
        </el-form-item>
        <el-form-item label="预约日期">
          <el-date-picker v-model="formInline.reservationDate" value-format="YYYY-MM-DD" type="date" style="width: 240px" />
        </el-form-item>
        <el-form-item label="随访人">
          <el-input clearable v-model="formInline.followUpBy" placeholder="" style="width: 180px" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="getlist">搜索</el-button>
          <!-- <el-button type="primary" @click="addbutton">新增</el-button> -->
          <el-button type="primary" @click="deletebutton">删除</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="contentbox">
      <el-table :data="datalist" border style="width: 100%" max-height="500" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="date" label="患者姓名" width="120">
          <template #default="scope">
            <span class="hzbox" @click="hzxqbutton(scope.row)">{{ scope.row.patientName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="phoneNumber" label="随访电话" />
        <el-table-column prop="followUpPurposeName" label="随访目的" />
        <el-table-column prop="followUpDate" label="随访日期" />
        <el-table-column prop="followUpMethodName" label="随访方式" />
        <el-table-column prop="followUpStatusName" label="随访状态" />
        <el-table-column prop="followUpBy" label="随访人" />
        <el-table-column prop="date" label="操作" width="120">
          <template #default="scope">
            <el-button type="primary" @click="xgbutton(scope.row.id)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="fybox">
        <el-pagination
          v-model:current-page="formInline.pageNum"
          v-model:page-size="formInline.pageSize"
          :page-sizes="[10, 50, 100, 200]"
          layout="total, sizes, prev, pager"
          :total="pagetotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog v-model="dialogVisible" title="患者随访" width="600" :before-close="handleClose">
      <el-form :model="form" label-width="auto" style="max-width: 600px" ref="ruleFormRef">
        <!-- <el-form-item label="姓名" prop="remark">
          <el-input v-model="form.patientName" />
        </el-form-item> -->

        <el-form-item label="随访方式" prop="followUpMethod">
          <el-select v-model="form.followUpMethod">
            <el-option v-for="item in ssfslist" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="随访目的" prop="followUpPurpose">
          <el-radio-group v-model="form.followUpPurpose">
            <el-radio v-for="option in sfmdlist" :key="option.dictValue" :value="option.dictValue" size="large">
              {{ option.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="随访状态" prop="followUpStatus">
          <el-select v-model="form.followUpStatus">
            <el-option v-for="item in sfstatus" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="随访日期">
          <el-date-picker v-model="form.followUpDate" value-format="YYYY-MM-DD" type="date" style="width: 240px" />
        </el-form-item> -->

        <el-form-item label="患者主诉">
          <el-input v-model="form.patientComplaint"></el-input>
        </el-form-item>
        <el-form-item label="解决方案">
          <el-input v-model="form.solution"></el-input>
        </el-form-item>
        <el-form-item label="预约日期">
          <el-date-picker v-model="form.reservationDate" value-format="YYYY-MM-DD" type="date" style="width: 240px" />
        </el-form-item>
        <!-- <el-form-item label="随访人">
          <el-input v-model="form.followUpBy"></el-input>
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" />
        </el-form-item>

        <el-form-item>
          <div class="savrnutton">
            <el-button type="primary" @click="savebutton(ruleFormRef)">保存</el-button>
            <el-button @click="clearbutton(ruleFormRef)">清空</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup name="home">
import { ref, reactive } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { addsfAPI, updatesfAPI, getsflistApi, deltesfApi, sfgetInfoApi } from "@/api/hzmange/index.js";
import { getCurrentDateFormatted } from "@/utils/index";
const route = useRoute();
const routedata = reactive(route.query);
const YZlist = JSON.parse(sessionStorage.getItem("YZlist"));
let ssfslist = ref([]); //随访方式
let sfmdlist = ref([]); //随访目的
let sfstatus = ref([]); //随访状态
sfmdlist.value = YZlist.follow_up_purpose;
ssfslist.value = YZlist.follow_up_method;
sfstatus.value = YZlist.follow_up_status;
const router = useRouter();
const activeName = ref("1");
let dialogVisible = ref(false);
const handleClick = e => {
  console.log(11111111);
};
let datalist = ref([
  {
    date: "张三",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles"
  },
  {
    date: "2016-05-02",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles"
  },
  {
    date: "2016-05-04",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles"
  },
  {
    date: "2016-05-01",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles"
  }
]);
const zztdvalue = ref("");
const options = [
  {
    value: "Option1",
    label: "Option1"
  },
  {
    value: "Option2",
    label: "Option2"
  },
  {
    value: "Option3",
    label: "Option3"
  },
  {
    value: "Option4",
    label: "Option4"
  },
  {
    value: "Option5",
    label: "Option5"
  }
];
let selection = ref([]);
const handleSelectionChange = e => {
  selection.value = e;
};
const rules = reactive({
  sbh: [{ required: true, message: "请输入", trigger: "blur" }],
  name: [{ required: true, message: "请输入", trigger: "blur" }],
  phone: [{ required: true, message: "请输入", trigger: "blur" }],
  xty: [{ required: true, message: "请选择", trigger: "change" }],

  zrwj: [
    {
      required: true,
      message: "请选择",
      trigger: "change"
    }
  ]
});
var pagetotal = ref(0);
let handleSizeChange = () => {
  getlist();
};
let handleCurrentChange = () => {
  getlist();
};
const ruleFormRef = ref(null);
const formInline = ref({
  pageNum: 1,
  pageSize: 10,
  patientName: "", //患者姓名
  followUpMethod: "", //随访方式
  followUpPurpose: "", //随访目的
  followUpStatus: "", //随访状态
  followUpDate: "", //随访日期
  reservationDate: getCurrentDateFormatted(), //预约日期
  followUpBy: "" //随访人
});
let currentPage = ref(1);
let pageSize = ref(10);
let addbutton = () => {
  dialogVisible.value = true;
};
const handleClose = () => {
  dialogVisible.value = false;
};

let hzxqbutton = e => {
  console.log(e);
  router.push({
    path: "/hzinfo",
    query: {
      id: e.patientId,
      name: e.patientName
    }
  });
};
const form = ref({
  patientName: "", //患者姓名
  followUpMethod: "", //随访方式
  followUpPurpose: "", //随访目的
  followUpStatus: "", //随访状态
  followUpDate: "", //随访日期
  reservationDate: "", //预约日期
  followUpBy: "", //随访人
  remark: "" //备注
});
const searchbutton = () => {
  console.log("submit!");
};
let xgstatus = ref(false);
function savebutton(formEl) {
  if (xgstatus.value == true) {
    updatesfAPI(form.value).then(() => {
      ElMessage({
        message: "修改成功",
        type: "success"
      });
      formEl.resetFields();
      dialogVisible.value = false;
      xgstatus.value = false;
      //刷新列表数据
      getlist();
    });
  } else {
    //新增
    addsfAPI(form.value).then(() => {
      ElMessage({
        message: "新增成功",
        type: "success"
      });
      formEl.resetFields();
      dialogVisible.value = false;
      getlist();
    });
  }
}
const resetForm = formEl => {
  if (!formEl) return;
  formEl.resetFields();
  dialogVisible.value = false;
};
getlist();
function getlist() {
  getsflistApi(formInline.value).then(res => {
    pagetotal.value = res.total;
    datalist.value = res.rows;
  });
}
const xgbutton = e => {
  sfgetInfoApi({
    id: e
  }).then(res => {
    dialogVisible.value = true;
    form.value = res.data;
    xgstatus.value = true;
  });
};
const deletebutton = () => {
  if (selection.value.length == 1) {
    ElMessageBox.confirm("确定删除?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    }).then(() => {
      deltesfApi({
        id: selection.value[0].id
      }).then(res => {
        ElMessage({
          type: "success",
          message: `${res.msg}`
        });
        getlist();
      });
    });
  } else {
    ElMessage({
      message: "请选择一条数据",
      type: "warning"
    });
  }
};
</script>

<style scoped lang="scss">
.home {
  width: 100%;
  height: 100%;

  .contentbox {
    width: 100%;
    height: 300px;
  }

  .szbox {
    justify-content: end;
    display: flex;
  }
}

.card {
  padding: 25px;
}

.hzbox {
  color: #409eff;
  cursor: pointer;
}

.savebox {
  display: flex;
  justify-content: center;
}
.fybox {
  display: flex;
  justify-content: end;
}
</style>
